package yanglib.membership;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 9/5/14
 * Time: 1:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class Membership {

    private HashMap<Integer, Properties> idToProperties = new HashMap<Integer, Properties>();
    private HashMap<String, HashMap<Integer, Properties>> roleToProperties = new HashMap<String, HashMap<Integer, Properties>>();

    public Membership(File file){
        readFromFile(file);
    }

    private void readFromFile(File file){
        try{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            System.out.println(file.getAbsolutePath());
            Document document =
                    builder.parse(new FileInputStream(file));
            NodeList nodeList = document.getDocumentElement().getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if(node instanceof Element){
                    if(node.getNodeName().equals("node")){
                        Properties p = new Properties();
                        for(int j=0; j<node.getChildNodes().getLength(); j++){
                            Node child = node.getChildNodes().item(j);
                            if(child instanceof Element){
                                //System.out.println(child.getNodeName()+" " + child.getTextContent());
                                p.setProperty(child.getNodeName(), child.getTextContent());
                            }

                        }
                        int id = Integer.parseInt(p.getProperty("id"));
                        String role = p.getProperty("role");
                        int roleid = Integer.parseInt(p.getProperty("roleid"));
                        if(idToProperties.containsKey(id))
                            throw new RuntimeException("Duplicate id "+id+" in configuration file");
                        idToProperties.put(id, p);
                        if(roleToProperties.containsKey(role) && roleToProperties.get(role).containsKey(roleid))
                            throw new RuntimeException("Duplicate role and roleid ("+role+"."+roleid+")");
                        if(!roleToProperties.containsKey(role))
                            roleToProperties.put(role, new HashMap<Integer, Properties>());
                        roleToProperties.get(role).put(roleid, p);
                    }
                }

            }
        }
        catch(ParserConfigurationException e){
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public InetSocketAddress getAddress(int id){
        String host = getProperty(id, "host");
        int port = Integer.parseInt(getProperty(id, "port"));
        return new InetSocketAddress(host, port);
    }

    public int getRoleCount(String role){
        return roleToProperties.get(role).size();
    }

    public String getProperty(int id, String key){
        return (String)idToProperties.get(id).get(key);
    }

    public String getProperty(String role, int roleid, String key){
        return (String)roleToProperties.get(role).get(roleid).get(key);
    }

    public void addAddress(int id, InetSocketAddress address){

    }

    public static void main(String []args) throws Exception{
        Membership membership = new Membership(new File("D:\\project-of-yang\\library\\config.xml"));
    }
}
